package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Floor;
import cn.zyy.hotel.service.FloorService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.FloorVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/floor")
public class FloorController {

    @Resource
    private FloorService floorService;
    @Resource
    private RedisTemplate redisTemplate;

    //查询楼层列表
    @RequestMapping("/list")
    public Result list(FloorVO floorVo){
        //设置分页信息
        PageHelper.startPage(floorVo.getPage(),floorVo.getLimit());
        List<Floor> floorList;
        if (redisTemplate.hasKey("floorList")){
            floorList = redisTemplate.opsForList().range("floorList",0,-1);
        }else {
            //调用查询楼层列表的方法
            floorList = floorService.selectFloorList(floorVo);
            redisTemplate.opsForList().rightPushAll("floorList",floorList);
        }
        //创建分页对象
        PageInfo<Floor> pageInfo = new PageInfo<Floor>(floorList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加楼层
    @RequiresPermissions("floor:add")
    @RequestMapping("/addFloor")
    public Object addFloor(Floor floor){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用添加楼层方法
        if (floorService.addFloor(floor)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
            if (redisTemplate.hasKey("floorList")){
                redisTemplate.delete("floorList");
            }
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改楼层
    @RequiresPermissions("floor:edit")
    @RequestMapping("/updateFloor")
    public Object updateFloor(Floor floor){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改楼层方法
        if (floorService.updateFloor(floor)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
            if (redisTemplate.hasKey("floorList")){
                redisTemplate.delete("floorList");
            }
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //查询当前楼层下是否存在房间
    @RequestMapping("/checkFloorHasRoom")
    public Object checkFloorHasRoom(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用查询楼层房间的方法
        if (floorService.getRoomCountByFloorId(id)>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该楼层下有房间，无法删除");
        }else {
            map.put(SystemConstant.EXIST,false);
        }
        return map;
    }

    //删除楼层
    @RequiresPermissions("floor:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除楼层的方法
        if (floorService.deleteById(id)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
            if (redisTemplate.hasKey("floorList")){
                redisTemplate.delete("floorList");
            }
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }

    //查询所有楼层
    @RequestMapping("/findAll")
    public Object findAll(){
        List<Floor> floorList;
        if (redisTemplate.hasKey("floorList")){
            floorList = redisTemplate.opsForList().range("floorList",0,-1);
        }else {
            //调用查询楼层列表的方法
            floorList = floorService.selectFloorList(null);
            redisTemplate.opsForList().rightPushAll("floorList",floorList);
        }
        return floorList;
    }

}
